<?php

class ZB_Walker_Nav_Menu extends Walker_Nav_Menu {

    public $db_fields = array('parent' => 'menu_item_parent', 'id' => 'db_id');
    protected $mega;

    public function __construct($mega = false) {
        $this->mega = $mega;
    }




    public function start_lvl( &$output, $depth = 0, $args = null ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat( $t, $depth );
        // 获取当前菜单项的设置
        $menu_mode = get_post_meta($this->current_item->ID, 'is_menu_div', true);



        $dropdown_class = 'menu-dropdown menu-single collapse';
        $dropdown_id = '';


        $id_attr = $dropdown_id ? ' id="' . esc_attr($dropdown_id) . '"' : '';
        $output .= "{$n}{$indent}<div class=\"{$dropdown_class}\"{$id_attr}><ul class=\"ul\">{$n}";
    }

    /**
     * Ends the list of after the elements are added.
     */
    public function end_lvl( &$output, $depth = 0, $args = null ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat( $t, $depth );
        $output .= "$indent</ul></div>{$n}";
    }

    /**
     * Starts the element output.
     */
    public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
        // 存储当前菜单项以便在start_lvl中使用
        $this->current_item = $item;

        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';

        // 只保留 menu-link 或 menusingle-li 类
        $classes = array();
        $classes[] = ($depth === 0) ? 'menu-link' : 'menusub-li';

        // 获取菜单项的自定义字段
        $is_menu_div = get_post_meta( $item->ID, 'is_menu_div', true );

        $site_top_menu=get_post_meta( $item->ID, 'site_top_menu', true );



        // 根据设置添加类
        if ($depth > 0) {
            $classes[] = 'menusingle-li';
        }

        $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        // 只在不是图片模式或不是子菜单项时输出li开始标签

        $output .= $indent . '<li' . $id . $class_names . '>';


        $atts           = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target ) ? $item->target : '';
        if ( '_blank' === $item->target && empty( $item->xfn ) ) {
            $atts['rel'] = 'noopener';
        } else {
            $atts['rel'] = $item->xfn;
        }
        $atts['href']         = ! empty( $item->url ) ? $item->url : '';
        $atts['aria-current'] = $item->current ? 'page' : '';

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
                $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $title = apply_filters( 'the_title', $item->title, $item->ID );
        $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

        $item_output = $args->before;


        // 处理图片模式
        if ( $depth==0&&$is_menu_div && !empty($site_top_menu)) {
            $link_class = ( $depth === 0 ) ? 'link-title' : 'menusingle-title';
            $item_output .= '<a' . $attributes . ' class="' . $link_class . '">';
            $item_output .= '<span class="sp-link-title">' . $args->link_before . $title . $args->link_after . '</span>';
            $item_output.= '<span class="menu-arrow"><i class="feather-chevron-down"></i></span>';
            $item_output .= '</a>';
            $item_output.= '<div class="menu-dropdown menu-banner collapse">';
            $item_output.= '<ul class="ul container p-0">';

           

            foreach ($site_top_menu as $item_nuem_img) {
                $image_url = $item_nuem_img['nenu_img_top']['url'];
                $image_alt = $item_nuem_img['nenu_img_top']['title'];
                $img_href =$item_nuem_img['href'];
                
                $item_output .= ' <li class="menubanner-li">';
                $item_output .= '<div class="menubanner-img banner-hover">';
                $item_output .= '<a href="' . esc_url($img_href) . '" target="_blank" class="collection-img banner-img">';
                $item_output .= '<img src="' . esc_url($image_url) . '" class="img-fluid" alt="' . esc_attr($image_alt) . '">';
                $item_output .= '</a>';
                $item_output .= '<a href="' . esc_url($img_href) . '" target="_blank" class="collection-title">';
                $item_output .= '<span>' . $args->link_before . $image_alt . $args->link_after . '</span>';
                $item_output .= '</a>';
                $item_output .= '</div>';
                $item_output .= '</li>';
            }
            $item_output .= '</ul>';
            $item_output .= '</div>';


        }else{
            // 判断当前菜单项是否有子菜单
            $children = get_posts( array(
                'post_type'   => 'nav_menu_item',
                'nopaging'    => true,
                'numberposts' => 1,
                'meta_key'    => '_menu_item_menu_item_parent',
                'meta_value'  => $item->ID,
            ) );
            // 常规菜单项
            $link_class = ( $depth === 0 ) ? 'link-title' : 'menusingle-title';
            $item_output .= '<a' . $attributes . ' class="' . $link_class . '">';
            $item_output .= '<span class="sp-link-title">' . $args->link_before . $title . $args->link_after . '</span>';
            if ( $depth === 0 && ! empty( $children ) ) {

                $item_output.= '<span class="menu-arrow"><i class="feather-chevron-down"></i></span>';
            }

            $item_output .= '</a>';
        }

        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }

    /**
     * Ends the element output, if needed.
     */
    public function end_el( &$output, $item, $depth = 0, $args = null ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }

        // 获取菜单项的自定义字段
        $is_menu_div = get_post_meta( $item->ID, 'is_menu_div', true );
        

        // 只在不是图片模式或不是子菜单项时输出li结束标签
        if (!($depth === 1 && $is_menu_div)) {
            $output .= "</li>{$n}";
        }
    }



    public static function fallback($args) {
        extract($args);


        $fb_output = null;

        if ($container) {
            $fb_output = '<' . $container;
            var_dump($fb_output);
            if ($container_id) {
                $fb_output .= ' id="' . $container_id . '"';
            }

            if ($container_class) {
                $fb_output .= ' class="' . $container_class . '"';
            }

            $fb_output .= '>';
        }

        $fb_output .= '<ul';

        if ($menu_id) {
            $fb_output .= ' id="' . $menu_id . '"';
        }

        if ($menu_class) {
            $fb_output .= ' class="' . $menu_class . '"';
        }

        $fb_output .= '>';
        $fb_output .= '<li class="menu-item"><a href="' . esc_url(admin_url('nav-menus.php')) . '">Add Menus</a></li>';
        $fb_output .= '</ul>';

        if ($container) {
            $fb_output .= '</' . $container . '>';
        }


        echo wp_kses($fb_output, array(
            'ul'   => array('id' => array(), 'class' => array()),
            'li'   => array('class' => array()),
            'a'    => array('href' => array()),
            'span' => array(),
        ));
    }

}

/**
 * 评论
 */
class ZB_Walker_Comment extends Walker_Comment {

    public function html5_comment( $comment, $depth, $args ) {
        $GLOBALS['comment'] = $comment;

        if ( 'div' == $args['style'] ) {
            $tag = 'div';
            $add_below = 'comment';
        } else {
            $tag = 'li';
            $add_below = 'div-comment';
        }
        $author = get_comment_author();

        if( $comment->user_id){

            $author = $author.' '.zb_get_user_badge($comment->user_id,'span');
        }else if( $comment->comment_author_url ){
            $author = '<a href="'.esc_url($comment->comment_author_url).'" target="_blank" rel="nofollow">'.$author.'</a>';
        }

        ?>
        <<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">

        <div id="div-comment-<?php comment_ID() ?>" class="comment-inner">
            <div class="comment-author">
                <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
            </div>
            <div class="comment-body">
                <div class="nickname"><?php echo $author;?></div>
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <div class="comment-awaiting-moderation"><?php _e('您的评论正在等待审核。', 'ripro'); ?></div>
                <?php endif; ?>
                <div class="comment-content"><?php comment_text(); ?></div>

                <div class="comment-meta">

                    <span class="comment-time"><?php echo sprintf(__('%s前', 'ripro'),human_time_diff(get_comment_time('U'), current_time('timestamp'))); ?></span>
                    <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
                </div>

            </div>


        </div>
        <?php
    }
}
